/*
 * Alexey Danchuk
 * This software is an implementation of the task from OOP module.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * This class describes wrapper for our knowledge system
 * and usage of different kinds of question types.
 *
 * @version 1.01 30 January 2013
 * @author Alexey Danchuk
 */
class KnowledgeWrapper {

	private static final int SIMPLE_ANSWER = 1;
	private static final int TEST_SIZE = 10;
	private static int studentMark = 0;

	private static Question[] systemQuestions = new Question[TEST_SIZE];

	private static String input() throws Exception {
		System.out.println(" input: ");
		BufferedReader inputStream = new BufferedReader(new InputStreamReader(
				System.in));
		String inputed = inputStream.readLine();
		// inputStream.close();
		return inputed;
	}

	private static String inputQuestion() throws Exception {
		System.out.print("Question for");
		String inQuestion = input();
		return inQuestion;
	}

	/**
	 * Method to input variants.
	 * Format: variant1 variant2...
	 * @return answers - variants
	 * @throws Exception
	 */
	private static String[] inputAnswers() throws Exception {
		String[] answers = null;
		int index = 0;
		System.out.print("Answers for");
		String inAnswer = input();
		int size = calculateSize(inAnswer);
		answers = new String[size];
		Scanner scanInput = new Scanner(inAnswer);
		while (scanInput.hasNext()) {
			answers[index] = scanInput.next();
			index++;
		}
		scanInput.close();
		return answers;
	}

	private static String[] inputCorrectAnswer() throws Exception {
		String[] correctAnswers = null;
		int index = 0;
		System.out.print("Correct answer(s) for");
		String inAnswers = input();
		int size = calculateSize(inAnswers);
		if (size > SIMPLE_ANSWER) {
			correctAnswers = new String[size];
			Scanner scanCorrect = new Scanner(inAnswers);
			while (scanCorrect.hasNext()) {
				correctAnswers[index] = scanCorrect.next();
				index++;
			}
			scanCorrect.close();
			return correctAnswers;
		} else {
			correctAnswers = new String[SIMPLE_ANSWER];
			correctAnswers[0] = inAnswers;
			return correctAnswers;
		}
	}

	/**
	 * Method to calculate arrays size.
	 * @param string - string to array
	 * @return size - size for new array
	 */
	private static int calculateSize(String string) {
		int size = 0;
		Scanner sizeScan = new Scanner(string);
		while (sizeScan.hasNext()) {
			sizeScan.next();
			size++;
		}
		sizeScan.close();
		return size;
	}

	/**
	 * Method to detect question type.
	 * @return question - one of three types of questions
	 */
	private static Question typeDetect() {
		String inQuestion = null;
		String[] inAnswers = null;
		String[] correctAnswer = null;
		Question question = null;
		try {
			inQuestion = inputQuestion();
			inAnswers = inputAnswers();
			correctAnswer = inputCorrectAnswer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (correctAnswer.length > SIMPLE_ANSWER) {
			question = new DiverseQuestion(inQuestion, inAnswers,
					                       correctAnswer);
			return question;
		} else {
			if ((correctAnswer[0].equals("true"))
				|| (inAnswers[0].equals("false"))) {
				question = new LogicalQuestion(inQuestion, inAnswers,
						correctAnswer);
				return question;
			} else {
				question = new SimpleQuestion(inQuestion, inAnswers,
						correctAnswer);
				return question;
			}
		}
	}

	/**
	 * Method to fill the array of questions for the test.
	 */
	public static void initTestSystem() {
		for (int i = 0; i < TEST_SIZE; i++) {
			systemQuestions[i] = typeDetect();
		}
	}

	private static String[] inputUserAnswers() throws Exception {
		String[] userAnswers = inputAnswers();
		return userAnswers;
	}

	private static void printQuestions(Question[] systemQuestions) {
		System.out.println("Question is: ");
		for (int i = 0; i < systemQuestions.length; i++) {
			System.out.println(systemQuestions[i].outQuestionText());
			printAnswersVariants(systemQuestions[i]);
			try {
				String[] userAnswers = inputUserAnswers();
				studentMark += systemQuestions[i].chekAnswer(userAnswers);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void printAnswersVariants(Question actualQuestion) {
		String[] choices = actualQuestion.getAnswerChoices();
		for (String choice : choices) {
			System.out.println("Variant: " + choice);
		}
	}

	public static void takeTheTest() {
		printQuestions(systemQuestions);
		System.out.println(getStudentMark());
	}

	private static int getStudentMark() {
		return studentMark;
	}

	/**
	 * Main method. Entry point into the program.
	 * @param args - command-Line arguments
	 */

	public static void main(String[] args) {
		// For testing only
		initTestSystem();
		takeTheTest();
	}

}
